#!/bin/bash
#
# vbox-update <dkms top level>

#
# NOTE: update vbox/BOM if you get the source from anywhere other than
# the archive.
#
# To update:
#  TMP=$HOME/tmp
#  wget -O $TMP/virtualbox-guest-dkms.deb http://ports.ubuntu.com/pool/multiverse/v/virtualbox/virtualbox-guest-dkms_4.3.22-dfsg-1_all.deb
#  dpkg-deb -R $TMP/virtualbox-guest-dkms.deb $TMP/virtualbox-guest-update
#  vbox-update $TMP/virtualbox-guest-update
#
# To test build:
#  fakeroot debian/rules prepare-generic
#  make O=debian/build/build-generic/ ubuntu/vbox/vboxguest/vboxguest.ko ubuntu/vbox/vboxvideo/vboxvideo.ko ubuntu/vbox/vboxsf/vboxsf.ko
#

if [ "$#" -ne 1 ]; then
	echo "Usage: $0 <unpacked vbox dkms guest directory>" 1>&2
	exit 1
fi
vbox="$1"

# Update vbox ...
git rm -rf vbox
mkdir vbox
cp -rp "$vbox/usr/src/"*/* vbox

# Work out what version this represents.
ver=`awk '($1 == "Version:") { print $2 }' <"$vbox/DEBIAN/control"`

# Fix up the KBUILD_EXTMOD as we are not building externally.
for make in vbox/*/Makefile
do
	sed -i -e '1iKBUILD_EXTMOD=${srctree}/ubuntu/vbox' $make
done

# Fix up KERN_DIR to point at the kernel source tree
for make in vbox/*/Makefile.include.header
do
	sed -i -e 's/^KERN_DIR := .*$/KERN_DIR := $(srctree)/' $make
done

# We're now using the vboxvideo module in drivers/staging, so disable
# the version that was imported.
sed -i -e 's: vboxvideo/::' vbox/Makefile

# Reduce the version of the in-kernel module just slightly so it
# will not clash with the dkms package.
for version in vbox/*/version-generated.h
do
	sed -i -e 's/_Ubuntu"/_KernelUbuntu"/' $version
done

# Record the version number and nominal source.
{
	echo "Source: http://ports.ubuntu.com/pool/multiverse/v/virtualbox/virtualbox-guest-dkms_${ver}_all.deb"
	echo "Version: $ver"
} >vbox/BOM

git add vbox
{
	echo "UBUNTU: ubuntu: vbox -- update to $ver"
} | git commit -s -F -
